package com.fruit.util;

import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
 * HttpClientUtil
 * @author Administrator 说明：日期处理 创建人：FH Q313596790
 * @version 2017.12.01 11:40
 */
public class HttpClientUtil {

	private static PoolingHttpClientConnectionManager cm = null;

	/**
	 * HttpClientUtil
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public HttpClientUtil() {
		LayeredConnectionSocketFactory sslsf = null;
		try {
			sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
				.register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();
		cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
		cm.setMaxTotal(200);
		cm.setDefaultMaxPerRoute(20);
	}

	/**
	 * getHttpClient
	 * @return  CloseableHttpClient
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public static CloseableHttpClient getHttpClient() {
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

		/*
		 * CloseableHttpClient httpClient =
		 * HttpClients.createDefault();//如果不采用连接池就是这种方式获取连接
		 */
		return httpClient;
	}

	/**
	 *  块钱
	 * @param factory factory
	 * @return  CloseableHttpClient
	 * @author Administrator 说明：日期处理 创建人：FH Q313596790
	 * @version 2017.12.01 11:40
	 */
	public static CloseableHttpClient getHttpClient(LayeredConnectionSocketFactory factory) {
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setSSLSocketFactory(factory)
				.build();

		/*
		 * CloseableHttpClient httpClient =
		 * HttpClients.createDefault();//如果不采用连接池就是这种方式获取连接
		 */
		return httpClient;
	}

}
